package app.demo;

import java.text.DecimalFormat;
import java.util.Hashtable;

import app.kernel.api.McsAPI;
import config.PSG_Configuration;


public class demo {

	// To keep track of time.
	public static double curr;
	public static int Count=4;
	public static int maxN = 100;
	public static int Ndiscoverer = 1;
	public static McsAPI ps1;
	//public static McsAPI ps2;
	public static McsAPI ps3;
	public static McsAPI ps4;
	public static McsAPI ps5;
	public static Hashtable PSGs = new Hashtable();
	public static String defaultIP =PSG_Configuration.defaultIP;
	public static int defaultPort = PSG_Configuration.defaultPort;
	public static String defaultName =PSG_Configuration.PSGName;
	public static String ID_KEY = "http://"+defaultIP+ ":"+(defaultPort+(0))+"/xmlrpc";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		
		
		
		// TODO Auto-generated method stub
		int maxN = 1; // Number of PSGs generated.
		int Ndiscoverer = 1;
		int calib = 0;
		curr = System.currentTimeMillis();
	    System.out.println("Start " + "Time: "+curr);			

		
		for (int i=0; i<maxN; i++){
			

			
			
			PSGs.put(ID_KEY, new McsAPI(ID_KEY, defaultIP, defaultPort+(i+calib)));
			System.out.println("PSGs.get["+ID_KEY+"] = "+ID_KEY+"; PSG.contains = "+PSGs.containsKey(ID_KEY));
			
			((McsAPI)PSGs.get(ID_KEY)).sm.cds.assign("person", "name", "HOME" + new Integer(i).toString());
			//DecimalFormat df2 = new DecimalFormat("#########0.000");
			//((McsAPI)PSGs.get(ID_KEY)).sm.cds.assign("home", "egg_price", new Double(df2.format(10000*Math.random())).toString());
			curr = System.currentTimeMillis();
			//((McsAPI)PSGs.get(ID_KEY)).sm.register(ID_KEY);

		    double time = (System.currentTimeMillis()-curr);				    
		    System.out.println("                 Time Taken: "+ time);
		    System.out.println("Request-Response Per Second: "+ 1000/(time/demo.maxN/demo.Ndiscoverer));

			System.out.println();
			System.out.println("--------------------------------------------------------------------------------");
			System.out.println("--------------------------------------------------------------------------------");
			System.out.println("--------------------------------"+i+"-----------------------------------------------");
			System.out.println("--------------------------------------------------------------------------------");
			System.out.println("--------------------------------------------------------------------------------");
			
			Thread.sleep(150);
			
		}
/*
		for (int i=0; i<maxN/2; i++){
			

			
			String ID_KEY = "http://"+defaultIP+ ":"+(defaultPort+(i+calib))+"/xmlrpc";
			PSGs.put(ID_KEY, new McsAPI(ID_KEY, defaultIP, defaultPort+(i+calib)));
			System.out.println("PSGs.get["+ID_KEY+"] = "+ID_KEY+"; PSG.contains = "+PSGs.containsKey(ID_KEY));
			
			((McsAPI)PSGs.get(ID_KEY)).sm.cds.assign("shop", "name", "SHOP" + new Integer(i).toString());
			((McsAPI)PSGs.get(ID_KEY)).sm.cds.assign("shop", "egg_price", new Integer((int)(100*Math.random())).toString());
			((McsAPI)PSGs.get(ID_KEY)).sm.register("supermarket"+";"+ID_KEY);
			Thread.sleep(100);
			
		}*/
/*
		for (int i=0; i<maxN-1; i++){
			

			
			String ID_KEY = "http://"+defaultIP+ ":"+(defaultPort+(i+calib))+"/xmlrpc";
			//PSGs.put(ID_KEY, new McsAPI(ID_KEY, defaultIP, defaultPort+(i+calib)));
			//System.out.println("PSGs.get["+ID_KEY+"] = "+ID_KEY+"; PSG.contains = "+PSGs.containsKey(ID_KEY));
			
			((McsAPI)PSGs.get(ID_KEY)).sm.leave(ID_KEY);
			Thread.sleep(500);
			
		}
*/
		//ps2 =  new McsAPI("192.168.1.105",10002);
		//ps2 = new McsAPI(defaultName, defaultIP,defaultPort);
		//ps1 = new McsAPI("Keith","192.168.1.103",10003);
		//ps3 = new McsAPI("Justina","192.168.1.103",10004);
		//ps4 = new McsAPI("Office","192.168.1.103",10005);
		//ps5 = new McsAPI("VivoCity","192.168.1.103",10006);
	
		//ps2.createSC("home", "name", "http://192.168.1.103:9081/xmlrpc/");
		
		//ps1.register_PSG("home", "temp");
		//ps3.register_PSG("home", "temp");
		//ps4.register_PSG("home", "temp");
		//ps5.register_PSG("home", "temp");
		//ps2.register_PSG("person", "name");
		//ps2.register_PSG("person", "location");
		//ps2.register_PSG("shop", "name");
		//ps2.register_PSG("office", "isVacant");
		//ps2.leave_PSG("home", "temp");
		//ps2.leave_PSG("home", "abc");
		//ps2.leave_PSG("home", "light");
		//ps2.leave_PSG("home", "xyz");
		//ps2.leave_PSG("home", "temp");
		
/*		ps2.register_PSG("home", "temp");
		ps2.register_PSG("home", "name");*/
		// Test Request and Response time.
		for (int i=0; i<maxN; i++) {
			
			//System.out.println(ps2.queryMCS("SELECT name from SHOP where hasAdvertisement = \"false\""));			
			//System.out.println(ps2.queryMCS("SELECT location from PERSON where name = \"Keith\""));			
			//System.out.println(ps2.queryMCS("SELECT location from PERSON where name = \"Ben\""));			
			//System.out.println(ps2.queryMCS("SELECT location from PERSON where name = \"Justina\""));
			
			//System.out.println(ps2.queryMCS("person", "name", "SELECT name, location from PERSON where name = \"Keith\""));
		      
		}
		
	    double time = (System.currentTimeMillis()-curr);				    
	    System.out.println("                 Time Taken: "+ time);
	    System.out.println("Request-Response Per Second: "+ 1000/(time/demo.maxN/demo.Ndiscoverer));
	    //System.out.println(ps2.queryMCS("person", "name", "SELECT name, location from PERSON where name = \"Ben\""));
	    
	    	    
	    //System.out.println(ps2.queryMCS("office", "name", "SUBSCRIBE isVacant FROM OFFICE WHERE name = \"ConferenceRoom\""));
	    
		//myDiscoverer.queryMCS("HomeSpace", "CCbc", "Select ABC from cba where xyz=true;");

		
		
	}

}
